package com.lw.mall.service2.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.mall.entity.OrderItem;
import com.lw.mall.entity.Product;
import com.lw.mall.entity.User;
import com.lw.mall.filter.AuthorizationFilter;
import com.lw.mall.mapper.OrderItemMapper;
import com.lw.mall.mapper.ProductMapper;
import com.lw.mall.service2.OrderItemService2;
import com.lw.mall.vo.SellDetailListVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lw
 * @since 2022-09-25
 */
@Service("orderItemServiceImpl2")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements OrderItemService2 {

    @Resource
    private ProductMapper productMapper;

    @Override
    public List<SellDetailListVo> geSellDetailList(int orderId) {
        final User user = AuthorizationFilter.userLogin.get();
        final List<OrderItem> list =
                this.list(new LambdaQueryWrapper<OrderItem>().
                        eq(OrderItem::getOrderId, orderId).eq(OrderItem::getSellId,user.getId()));
        final List<SellDetailListVo> collect = list.stream().map(item -> {
            final Integer productId = item.getProductId();
            final Product product = productMapper.selectById(productId);
            final SellDetailListVo sellDetailListVo = new SellDetailListVo();
            sellDetailListVo.setCount(item.getCount());
            sellDetailListVo.setSubTotal(item.getSubTotal());
            sellDetailListVo.setImage(product.getImage());
            sellDetailListVo.setTitle(product.getTitle());
            return sellDetailListVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
